<?php
class MView {
    private $_module;
    private $_controller;
    private $_action;
    private $_scripts;
    private $_stylesheets;
    private $_isAdmin = false;

    public function  __construct($module,$controller,$action) {
        $this->_module = $module;
        $this->_controller = $controller;
        $this->_action = $action;
        $this->_scripts = array();
        $this->_stylesheets = array();
    }

    /**
     * Add a script to the view
     * @param String $scriptSrc : Relative path to the script
     */
    public function addScript($scriptSrc) {
        array_push($this->_scripts, $scriptSrc);
    }

    /**
     * Add a stylesheet to the view
     * @param String $stylesheetSrc : Relative path to the stylesheet
     */
    public function addStylesheet($stylesheetSrc) {
        array_push($this->_stylesheets, $stylesheetSrc);
    }

    /**
     * Renders XHTML Code for all registered scripts
     * @return String : XHTML code
     */
    public function renderScripts(){
        $return = NULL;
        foreach ($this->_scripts as $script){
            $return .= MHtml::tag('script', array('type'=>'text/javascript', 'src'=>$script));
        }
        return $return;
    }

    /**
     * Render XHTML Code for all registered stylesheets
     * @return String : XHTML Code
     */
    public function renderStylesheets(){
        $return = NULL;
        foreach ($this->_stylesheets as $style){
            $return .= MHtml::tag('link', array('rel'=>'stylesheet', 'type'=>'text/css', 'href'=>$style));
        }
        return $return;
    }

    /**
     * Render XHTML Code of a view
     * @param String $file (optional) : The name of the file to render
     * @param Array $data (optional) : Parameters/Variables of the view
     * @param String $path (optional) : The directory path to find the $file view. If this parameter is set, you also have to set the $file parameter.
     * @return String : XHTML Code of the view 
     */
    public function render($file = NULL, $data = array(), $path = NULL){
        extract($data);
        if ($file === NULL){
            // TODO : Check if the path exists
            if ($this->_isAdmin){
                $viewFile = BASE_PATH.DS.Mandrill::getApp()->modulesFolder.DS.$this->_module.DS.'admin'.DS.'views'.DS.$this->_controller.DS.$this->_action.'.php';
            } else {
                $viewFile = MTemplate::getInstance()->getAbsoluteDirectoryPath().DS.'views'.DS.$this->_controller.DS.$this->_action.'.php';
            }
        } else {
            // TODO : Check if the path exists
            if ($path !== NULL){
                $viewFile = $path.DS.$file.'.php';
            } else {
                if ($this->_isAdmin){
                    $viewFile = BASE_PATH.DS.Mandrill::getApp()->modulesFolder.DS.$this->_module.DS.'admin'.DS.'views'.DS.$this->_controller.DS.$file.'.php';
                } else {
                    $viewFile = MTemplate::getInstance()->getAbsoluteDirectoryPath().DS.'views'.DS.$this->_controller.DS.$file.'.php';
                }
            }
        }
        ob_start();
        require ($viewFile);
        $body = ob_get_contents();
        ob_end_clean();
        return $body;
    }
    
    /**
     * Check the existence of the view
     * @return Boolean : Existence of the view
     */
    public function exists(){
        $viewFile = MTemplate::getInstance()->getAbsoluteDirectoryPath().DS.'views'.DS.$this->_controller.DS.$this->_action.'.php';
//        $viewFile = BASE_PATH.DS.Mandrill::getApp()->modulesFolder.DS.$this->_module.DS.'views'.DS.$this->_controller.DS.$this->_action.'.php';
        return file_exists($viewFile);
    }
    
    public function isAdmin($isAdmin = FALSE){
        if ($isAdmin !== FALSE)
            $this->_isAdmin = $isAdmin;
        return $this->_isAdmin;
    }
    
    public function __destruct() {
        unset($this->_scripts);
        unset($this->_stylesheets);
    }
}
?>
